package com.tenglong.teacher.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.tenglong.teacher.R;

/**
 * @author hdx on 2019/2/26.
 */
public class VersionDialog implements View.OnClickListener {
    private final TextView mVersionCode;
    private View mView;
    private Context context;
    private Dialog mDialog;
    private VersionDialogInterface mListener;

    public VersionDialog(Context context) {
        this.context = context;
        mView = LayoutInflater.from(context).inflate(R.layout.new_version_dialog, null);
        mView.findViewById(R.id.mNegativeButton).setOnClickListener(this);
        mView.findViewById(R.id.mPositiveButton).setOnClickListener(this);
        mVersionCode = mView.findViewById(R.id.mVersionCode);
    }

    public void showDialog(String versionName) {
        if (mDialog == null) {
            mDialog = new Dialog(context);
            mDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            mDialog.addContentView(mView, new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT));
            mDialog.setCancelable(false);
        }
        mVersionCode.setText("发现新版本：" + versionName);
        mDialog.show();
        mDialog.setCanceledOnTouchOutside(false);
    }

    public void closeDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    public void setOnDialogListener(VersionDialogInterface dialogListener){
        mListener = dialogListener;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mNegativeButton:
                mListener.onSyntony(VerSionType.DIS_VERSION_TYPE);
                break;
            case R.id.mPositiveButton:
                /**
                 * TODO 后续操作
                 */
                mListener.onSyntony(VerSionType.CON_VERSION_TYPE);
                break;
        }
        closeDialog();
    }
    public interface VersionDialogInterface{
        void onSyntony(VerSionType type);
    }

    public enum VerSionType {
        DIS_VERSION_TYPE,
        CON_VERSION_TYPE
    }
}

